package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.pojo.Brand;
import com.pinyougou.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

//这个注解 内包含controller和ResponseBody ResponseBody
@RestController
@RequestMapping("/brand")
public class BrandController {

    //引用服务接口对象
    //timeout:调用服务接口超时时间毫秒值
    @Reference(timeout = 10000)
    private BrandService brandService;



    @PostMapping("/save")
    public boolean saveOrUpdate(@RequestBody Brand brand){
        try {
            brandService.save(brand);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }
    @PostMapping("/update")
    public boolean update(@RequestBody Brand brand){

        try {
            brandService.update(brand);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
   //多条件分页查询品牌
    @GetMapping("/findByPage")
    public PageResult findByPage(Brand brand, Integer page,
                                 @RequestParam(defaultValue = "10") Integer rows) {
        // {pages : 100, rows : [{},{}]}
        // Map|实体类
        /** GET请求中文转码 */
        if (StringUtils.isNoneBlank(brand.getName())) {
            try {
                brand.setName(new String(brand.getName()
                        .getBytes("ISO8859-1"), "UTF-8"));
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return brandService.findByPage(brand, page, rows);
    }

    //删除品牌
    @GetMapping("/delete")
    public boolean delete(Long[]ids){

        try {
            brandService.deleteAll(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    //查询所有品牌
    @GetMapping("/findBrandList")
    public List<Map<String,Object>>findBrandList(){
        return brandService.findAllByIdAndName();
    }


}














